/*
Copyright 2014 Yury Kharlamov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated Inversion-Of-Control container
https://code.google.com/p/gen-ioc/

* * *

Shutdown Manager implementation
*/
#include "stdafx.h"
#include "GenIOC/ShutdownManager.h"
#include "GenIOC/EventSourceBase.h"
#include "GenIOC/EventDispatcher.h"

template<>
IfaceId GetTypeId<IShutdownManager>(){ return "IShutdownManager"; }

class ShutdownManager: public IShutdownManager, public IEventQueue
	, public EventSource, public ISetters
{
public:
	ShutdownManager() : m_shutdownStarted(false) { }

	void AddShutdownObject(const std::shared_ptr<IShutdown>& obj)
	{
		bool isShutdownStarted;
		{
			boost::lock_guard<boost::mutex> lock(m_mutex);
			m_objects.insert(obj);
			isShutdownStarted = m_shutdownStarted;
		}

		if (isShutdownStarted)
			obj->Shutdown();
	}

	void Shutdown() override
	{
		{
			boost::lock_guard<boost::mutex> lock(m_mutex);
			if (m_shutdownStarted)
				return;
			m_shutdownStarted = true;
		}
		m_shutdownThread = boost::thread([this]{ ShutdownThread(); });
	}

	void SetIfaceFactory(const IfaceId& ifaceId, const Factory& factory) override
	{
		if (ifaceId == ID_OF(IEventDispatcher))
			SetDispatcherFactory(factory);
	}

	void PushEvent(const boost::any& event) override
	{
		if (event.type() == typeid(ShutdownFinished))
		{
			const ShutdownFinished& sh = boost::any_cast<const ShutdownFinished&>(event);
			boost::lock_guard<boost::mutex> lock(m_mutex);
			auto it = std::find_if(m_objects.begin(), m_objects.end(),
				[&sh](const std::shared_ptr<IShutdown>& p){ return p.get() == sh.object; });
			if (it != m_objects.end())
				m_objects.erase(it);
		}

		boost::lock_guard<boost::mutex> lock(m_mutex);
		if (m_objects.empty() && m_shutdownStarted)
			DispatchEvent(ShutdownFinished(this));
	}
private:
	void ShutdownThread()
	{
		std::set<std::shared_ptr<IShutdown>> objects;
		{
			boost::lock_guard<boost::mutex> lock(m_mutex);
			objects = m_objects;
		}
		for each(const std::shared_ptr<IShutdown>& obj in objects)
			obj->Shutdown();
	}

	boost::mutex m_mutex;
	bool m_shutdownStarted;
	std::set<std::shared_ptr<IShutdown>> m_objects;
	boost::thread m_shutdownThread;
};

void AddShutdownManagerFactory(IObjectManager* objMan)
{
	IfaceIdList consumes, emits;
	consumes.push_back(ID_OF(IEventDispatcher));
	emits.push_back(ID_OF(IShutdownManager));
	objMan->AddFactory([]{return std::static_pointer_cast<IShutdownManager>(std::make_shared<ShutdownManager>()); }, "ShutdownManager", consumes, emits, true);
}
